﻿@*
    ﻿//*********************************************************
    //
    // Copyright (c) Hewlett Packard. All rights reserved.
    // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
    // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
    // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
    // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
    //
    //*********************************************************
*@
@{
    ViewBag.Title = "Create and Upload a Document with Date Created";


    var recordTypes = this.TrimHelper.Search<RecordType>(
        BaseObjectTypes.RecordType,
        "behaviour:document",
        pageSize: 10, properties: new string[] { "Name" });
}

@section helpDescription {
    <p>The typical way to upload a document via the ServiceAPI is to use a multipart form post, unfortunately this does not upload the last modified or created dates. The result of the dates not being sent is that the record property Date Created is set to the time the document was uploaded.  It is possible to send the DateCreated property as part of the form, in addition it is possible to get the date last modified from the source file using Javascript.  This sample:</p>
    <ul>
        <li>has a simple record creation form including a hidden Date Created field,</li>
        <li>includes some Javascript to get the lastModifiedDate from the source file whenever the file upload field changes, and</li>
        <li>sets the DateCreated field from the file lastModifiedDate.</li>
    </ul>
    <p>Unfortunately it is not possible to get the file date created using the same method.</p>
    <h3>Example</h3>
    <p>This is the Javascript used to populate the DateCreated field with the file lastModifiedDate.</p>
    <pre>$(document).ready(function () {
    $('#file').on('change', function () {
        if (!window.File) {
            alert('Sorry, your browser doesn\'t support the File API so last modified date will not be available');
            return false;
        } else {
            var fileEl = $('#file')[0];
            if (fileEl.files.length > 0) {
                if (typeof fileEl.files[0].lastModifiedDate === 'undefined') {
                    alert('Sorry, your browser doesn\'t support the lastModifiedDate property so last modified date will not be available');
                    return false;
                } else {
                    var lmDate = fileEl.files[0].lastModifiedDate;
                    $('#DateCreated').val(lmDate.toISOString());
                }
            }
        };
    });
});</pre>
}
<form class="trim-form" id="recordForm" method="post" action="~/Record" enctype="multipart/form-data">
    <input type="hidden" name="Continue" value="Record?q=uri:{0}" />
    <input type="hidden" id="DateCreated" name="@PropertyIds.RecordDateCreated" />
    <fieldset>
        <legend>Document details</legend>
        <label>Record Type:</label>
        <select name="@PropertyIds.RecordRecordType">
            @foreach (var recordType in recordTypes)
            {
                <option value="@recordType.Uri">@recordType.Name</option>
            }
        </select>
        <label>Title</label>
        <input name="@PropertyIds.RecordTitle" />
        <label>File:</label>
        <input type="file" id="file" name="Files"/>
        <button type="submit" class="btn">Submit</button>
    </fieldset>
</form>
@section scripts {
    @*Borrowed this from here: http://stackoverflow.com/questions/8285733/how-to-get-the-last-modified-date-of-the-uploaded-file*@    
    <script>
        $(document).ready(function () {
            $('#file').on('change', function () {

                if (!window.File) {
                    alert('Sorry, your browser doesn\'t support the File API so last modified date will not be available');
                    return false;
                } else {
                    var fileEl = $('#file')[0];
                    if (fileEl.files.length > 0) {
                        if (typeof fileEl.files[0].lastModifiedDate === 'undefined') {
                            alert('Sorry, your browser doesn\'t support the lastModifiedDate property so last modified date will not be available');
                            return false;
                        } else {
                            var lmDate = fileEl.files[0].lastModifiedDate;
                            $('#DateCreated').val(lmDate.toISOString());
                        }
                    }
                };
            });
        });
    </script>
}
